package com.example.commonexport.service.impl;

import com.example.commonexport.entity.ExportResult;
import com.example.commonexport.entity.ExportType;
import com.example.commonexport.entity.TaskStatus;
import com.example.commonexport.service.ExportService;
import com.example.commonexport.service.ExportTask;
import lombok.Data;

// 导出任务实现
@Data
public class ExportTaskImpl implements ExportTask {
    private final String taskId;
    private final long createTime;
    private final ExportType exportType;
    private TaskStatus status;
    private ExportResult result;
    private final ExportService exportService;

    public ExportTaskImpl(String taskId, ExportType exportType, ExportService exportService) {
        this.taskId = taskId;
        this.createTime = System.currentTimeMillis();
        this.exportType = exportType;
        this.status = TaskStatus.PENDING;
        this.exportService = exportService;
    }

    @Override
    public void execute() throws Exception {
        try {
            this.status = TaskStatus.PROCESSING;
            // 调用实际的导出服务执行导出
            this.result = exportService.exportData(this);
            this.status = TaskStatus.COMPLETED;
        } catch (Exception e) {
            this.status = TaskStatus.FAILED;
            this.result = new ExportResult(false, null, e.getMessage());
            throw e;
        }
    }
}    